
jQuery(document).ready(function() {
	
	var noticeId = getUrlParam('noticeId');	
		
	bindForm(noticeId);
			
	//失效实现显示
	$("#sxRadio").click(function(){		
		var a= $('#sxRadio input:radio:checked').val();
		if(a=="1"){
			$("#failDate").removeClass("hidden");
			$("#sx").removeClass("hidden");
		}else{
			$("#failDate").addClass("hidden");
			$("#sx").addClass("hidden");
		}
	})
	
	//失效时间范围
	$("#publishTime").change(function(){
		var time=new Date();
		if (new Date($(this).val()) >= time) {
			$("#asofDate").attr('min', $(this).val());
			$("#failDate").attr('min', $(this).val());
		} else {
			var year = time.getFullYear();
			var month = time.getMonth() + 1;
			var day = time.getDate();
			var dayStr = day > 9 ? day : '0' + day;
			var monthStr = month > 9 ? month : '0' + month;
			dateStr = year + "-" + monthStr + "-" + dayStr;
			$("#asofDate").attr('min', dateStr);
			$("#failDate").attr('min', dateStr);
		}				
		
	});
	
	$("#asofDate").change(function(){
		$("#failDate").attr('max', $(this).val());
	});
})


//初始化上传插件(上传)
function upload(id,noticeID,fileUrl,fileIds,config){
	$("#"+id).fileinput({ 
		language: 'zh', //设置语言
		browseLable:"选择上传...",
		enctype: 'multipart/form-data',
		initialPreview: fileUrl,
		allowedFileExtensions : ['jpg', 'png','gif','txt','sql','xlsx','xls','pdf','pptx','docx','doc','jpeg'],//接收的文件后缀
        uploadUrl:basePath()+"upload.do",
        showUpload: false, //是否显示上传按钮
        showCaption: false,//是否显示标题
        browseClass: "btn btn-primary", //按钮样式         
        overwriteInitial:false,
        previewFileIcon: '<i class="fa fa-file"></i>',
        uploadAsync:false, 
        initialPreviewShowDelete:true, 
        maxFileCount: 5,  
        maxFileSize: 1024*10, 
        layoutTemplates:{
        	actionUpload:'',
        	actionDrag:'',
        	actionZoom:''
        },   
        initialPreviewConfig: config,
        uploadExtraData: function(previewId, index){ //额外参数 返回json数组 
        	return{
        		'busscode':'01'
        }
        },       
             	            		  				 	                             				 			
	}).on("filebatchuploadsuccess", function(event, data) {
		var file=data.response.datas;
		var fileId="";
    	if(fileIds==null){
    		for (var i = 0; i < file.length; i++) {
				fileId+=file[i].fileid+","
		}
    		fileId=fileId.substring(0,fileId.length-1);
    	}else{
    		fileId=fileIds;
    		for (var i = 0; i < file.length; i++) {
				fileId+=","+file[i].fileid;
		}   		
    	}

    	//当上传成功回调函数     											
		$.ajax({
			method : 'post',
			url : basePath() + 'notice/fileId.do',
			data:{
				fileId:fileId,
				noticeId:noticeID
			},
			dataType : "json",
			success : function(data) {
				
			}
		})	 
    	parent.refreshData();		//刷新数据
	})
	
}

function save(){	
		var noticeData = $("#noticeForm").serializeObject();
		noticeData.orgCode = $("#orgSelect1").attr("data");
		if (!check(noticeData))
			return true;
		$.ajax({
			method : 'post',
			url : basePath() + 'notice/save.do',
			data : JSON.stringify(noticeData),
			dataType : "json",
			contentType : "application/json",
			success : function(data) {
				if (data.result == 1) {
					if($('#upload').fileinput("getFilesCount")>0){						
						$('#upload').fileinput('upload');											
					}else{
						parent.refreshData();		//刷新数据
					}								
					
				}
			}
		});
}	

function check(data) {
	var result = true;
	var mustStr = "title,symbol,contents,orgCode,publishTime,asofDate";
	var strMsg = "";
	var mustStrs = mustStr.split(',');
	for (var i = 0; i < mustStrs.length; i++) {
		var obj = $('[name="' + mustStrs[i] + '"]');
		var obj1 = obj.attr("placeholder");
		if (obj.val() == "") {
			strMsg += obj1.substring(0, 3)
					+ "<span style='color:red;'><strong>"
					+ obj1.substring(3, obj1.lengh) + "</strong></span>！<br />";
			result = false;
		}
	}
//	var a = $("[name='range']:checked").val();
//	if (a == null) {
//		strMsg += "请选择<span style='color:red;'><strong>发布范围</strong></span>！<br />"
//		result = false;
//	}
	if($("[name='top'][value='1']").prop("checked") && $("[name='failDate']").val()==""){
		strMsg += "请选择<span style='color:red;'><strong>置顶失效时间</strong></span>！<br />"
		result = false;	
	}
	if (strMsg != "") {
		window.wxc.xcConfirm(strMsg, window.wxc.xcConfirm.typeEnum.error);
	}
	return result;
}



function bindForm(noticeId){
	
	var row=parent.getNoticeDataByNoticeId(noticeId);
	var fileId= row.fileId;	

	$.ajax({
		method : 'post',
		url : basePath() + 'notice/getFileUrl.do',
		data:{
			fileId:fileId			
		},
		dataType : "json",
		success : function(data) {
			var html="";			
			var a=data.file;
			if(a!=null){
				var list = new Array();
				var config= new Array();
				for (var i = 0; i < a.length; i++) {
					var type=a[i].filetype.substring(0,5);
					var type1=a[i].filesuffix;
					if(type=="image"){
						html="<img class=\"file-preview-image kv-preview-data\" src=\"" + path()+a[i].fileurl +"\">";
						list[i]=html;
					}else if(type1=="txt"){
						html="<i class='fa fa-file-text-o  fa-5x'></i>";
						list[i]=html;	
					}else if(type1=="docx"){
						html="<i class='fa fa-file-word-o fa-5x'></i>";/*text-primary*/
						list[i]=html;	
					}else if(type1=="xlsx"){
						html="<i class='fa fa-file-excel-o fa-5x'></i>";/*text-success*/
						list[i]=html;	
					}else if(type1=="pptx"){
						html="<i class='fa fa-file-powerpoint-o fa-5x'></i>";
						list[i]=html;	
					}else if(type1=="pdf"){
						html="<i class='fa fa-file-pdf-o fa-5x'></i>";
						list[i]=html;	
					}else if(type1=="sql"){
						html="<i class='fa fa-file-word-o fa-5x'></i>";
						list[i]=html;	
					}										
					var fjson={
						caption: a[i].filename+"."+type1,// 展示的文件名
						url: basePath() + 'notice/deleteFile.do?noticeId='+noticeId,
						width: '120px',
						key:a[i].fileid,												
						extra: {id: 100}
					}
					config[i]=fjson;
				}		
				upload("upload",noticeId,list,fileId,config);
				
			}else{				
				upload("upload",noticeId,"",fileId,"");				
			}								
		}
	});
	
	if(row!=null){
		$("[name='noticeId']").val(row.noticeId);	
		$("[name='title']").val(row.title);
		$("[name='symbol']").val(row.symbol);
		$("[name='contents']").val(row.contents);
//		$("[name='range'][value='" + row.range + "']").prop("checked", true);
		$("[name='fileId']").val(row.fileId);
		$("[name='publishTime']").val(row.publishTime);
		$("[name='asofDate']").val(row.asofDate);
		$("[name='top'][value='" + row.top + "']").prop("checked", true);
		$("[name='failDate']").val(row.failDate);
		$("[name='theme']").val(row.theme);
		$("[name='label']").val(row.label);
		$("[name='evaluation'][value='" + row.evaluation + "']").prop("checked", true);
		bindOrg2('orgSelect1');
		$('#orgSelect1').val(row.orgName);
		$('#orgSelect1').attr("data",row.orgCode);
		if(row.top == "1"){
			$("#failDate").removeClass("hidden");
			$("#sx").removeClass("hidden");
		}
	}else{
//		bindOrg("orgSelect1","2","0","");
		bindOrg2('orgSelect1');
	}
	
	//验证失效时间
	var nowDate = new Date();
	var stime = $("[name='publishTime']").val();
	var etime= $("[name='asofDate']").val();	

	if (nowDate >= new Date(stime)) {
		var year = nowDate.getFullYear();
		var month = nowDate.getMonth() + 1;
		var day = nowDate.getDate();
		var dayStr = day > 9 ? day : '0' + day;
		var monthStr = month > 9 ? month : '0' + month;
		dateStr = year + "-" + monthStr + "-" + dayStr;
		$("#asofDate").attr('min', dateStr);
		$("#failDate").attr('min', dateStr);
		$("#failDate").attr('max', etime);
	} else {
		$("#asofDate").attr('min', stime);
		$("#failDate").attr('max', etime);
	}
	
}

function path(){
	var curWwwPath = window.document.location.href;
	var pathName = window.document.location.pathname;
	var pos = curWwwPath.indexOf(pathName);
	var localhostPath = curWwwPath.substring(0, pos);
	return localhostPath;
}
